#include "n3.h"
#include <stdio.h>


int read_statements(const char* filename, Statement** list) {
	Statement* s;
	FILE* f = fopen(filename, "r");
	if ( ! f ) {
		fprintf(stderr, "Cannot open file %s for reading\n", filename);
		return -1;
	}
	if ( !n3_read(f, list ) ) {
		fprintf(stderr, "Cannot read statements\n");
		fclose(f);
		return -1;
	}
	fclose(f);
	s = *list;
	while (s != NULL ) {
		fprintf(stdout, "s: %s\t p: %s\to: %s\n", s->s, s->p, s->o);
		s = s->next;
	}
	return 0;

}

int main(int argc, char* argv[]) {
	if ( argc > 1 ) {
		Statement* list = NULL;
		if (read_statements(argv[1], &list) ) {
			stmt_delete(list);
			return -1;
		}
		if (argc > 2 ) {
			Statement* questions = NULL;
			Statement* q;
			if ( read_statements(argv[2], &questions) ) {
				stmt_delete(questions);
				stmt_delete(list);
				return -2;
			} else {
				int have_notfound = 0;
				for ( q = questions; q != NULL; q = q->next) {
					Statement* s;
					int found = 0;
					for ( s = list; s != NULL; s = s->next ) {
						if ( stmt_match(s, q) ) {
							found = 1;
							break;
						}
					}
					if ( ! found  ) {
						have_notfound = 1;
						fprintf(stderr, "Statement <%s, %s, %s> not found!\n",
								q->s, q->p, q->o);
					}
				}
				stmt_delete(questions);
				if ( ! have_notfound ) {
					printf("OK\n");
				} else {
					printf("FAIL\n");
				}
			}
		}
		stmt_delete(list);
		return 0;
	
	} else {
		fprintf(stderr, "please put a filename in arg\n");
		return -1;
	}

}
